package com.liugh.service.zhongche.Imp;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.entity.standard.StandardCategory;
import com.liugh.entity.standard.StandardItem;
import com.liugh.mapper.zhongche.StandardCategoryMapper;
import com.liugh.mapper.zhongche.StandardItemMapper;
import com.liugh.service.zhongche.IStandardCategoryService;
import com.liugh.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Fatao on 2018/11/12 .
 */


@Service
public class StandardCategoryServiceImpl implements IStandardCategoryService {


    @Autowired
    private StandardCategoryMapper standardCategoryMapper;

    @Autowired
    StandardItemMapper itemMapper;


    /**
     * 增加一条分类 .
     * @param standardCategory .
     * @return .
     */
    public StandardCategory addStandardCategory(StandardCategory standardCategory) {
        return CommonUtil.isDBOpSuccess(standardCategoryMapper.insert(standardCategory)) ? standardCategory : null;
    }


    /**
     * 根据id更新分类 .
     * @param standardCategory .
     * @return .
     */
    public StandardCategory updateStandcategoryById(StandardCategory standardCategory) {
        return CommonUtil.isDBOpSuccess(standardCategoryMapper.updateById(standardCategory)) ? standardCategory : null;
    }


    /**
     * 根据id删除分类 .
     * @param id .
     * @return .
     */
    public int deleteCategoryById(int id) {
        return standardCategoryMapper.deleteById(id);
    }


    /**
     * 根据id获取分类 .
     * @param id .
     * @return .
     */
    public StandardCategory getCategoryById(int id) {
        return standardCategoryMapper.selectById(id);
    }


    /**
     * 分页获取所有的分类 .
     * @param page .
     * @return .
     */
    public Page<StandardCategory> getAllStandCategory(Page<StandardCategory> page) {
        return page.setRecords(standardCategoryMapper.selectPage(page, new EntityWrapper<>()));
    }

    /**
     * 获取category下的Item
     * @param page
     * @param id
     * @return
     */
    @Override
    public Page<StandardItem> getItemByCatId(Page<StandardItem> page, int id) {
        List<StandardItem> list = itemMapper.selectPage(page,new EntityWrapper<StandardItem>().where("category = {0}", id));
        for (StandardItem item : list) {
            item.setCategoryName(standardCategoryMapper.selectById(item.getCategory()).getCategoryName());
        }
        page.setRecords(list);
        return page;
    }


}
